<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="com.aliyuncs.utils.IOUtils" %>
<%@ page import="org.apache.http.HttpEntity" %>
<%@ page import="org.apache.http.client.methods.CloseableHttpResponse" %>
<%@ page import="org.apache.http.client.methods.HttpPost" %>
<%@ page import="org.apache.http.entity.StringEntity" %>
<%@ page import="org.apache.http.impl.client.CloseableHttpClient" %>
<%@ page import="org.apache.http.impl.client.HttpClientBuilder" %>
<%@ page import="org.apache.http.util.EntityUtils" %>
<%@ page import="org.dom4j.Document" %>
<%@ page import="org.dom4j.DocumentHelper" %>
<%@ page import="org.dom4j.Element" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%

    String requestid = request.getParameter("requestid");
    String tablename = "formtable_main_53";
    out.print(">>>>>>>>>>>>CheckAxmt410_icdWork>>>>>" + requestid + "<br/>");
    String method = "CheckAxmt410_icdWork";
    try {
        Map<String, String> masterMap = new HashMap<>();
        rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
        rs.next();
        String mainid = Util.null2String(rs.getString("id"));
        String ddlx = Util.null2String(rs.getString("ddlx"));//订单类型
        String xsdddh = Util.null2String(rs.getString("xsdddh"));//销售订单单号
        String ddrq = Util.null2String(rs.getString("ddrq"));//订单日期
        String ckkhbh = Util.null2String(rs.getString("ckkhbh"));//参考客户编号
        String shkh = Util.null2String(rs.getString("shkh"));//送货客户
        String shkhjc = Util.null2String(rs.getString("shkhjc"));//送货客户简称
        String xsddxz = Util.null2String(rs.getString("xsddxz"));//销售订单性质
        String khddh = Util.null2String(rs.getString("khddh"));//客户订单号
        String hszje = Util.null2String(rs.getString("hszje"));//含税总金额
        String sqrgh = Util.null2String(rs.getString("sqrgh"));//申请人工号
        String sqbmbm = Util.null2String(rs.getString("sqbmbm"));//申请部门
        String sz = Util.null2String(rs.getString("sz"));//税种
        String bz = Util.null2String(rs.getString("bz"));//币种
        String hl2 = Util.null2String(rs.getString("hl2"));//汇率2
        String jgtj = Util.null2String(rs.getString("jgtj"));//价格条件
        String sktj = Util.null2String(rs.getString("sktj"));//收款条件
        String sqzje = Util.null2String(rs.getString("sqzje"));//税前总金额
        String hth = Util.null2String(rs.getString("hth"));//合同号
        String zlly = Util.null2String(rs.getString("zlly"));//资料来源

        masterMap.put("oea00", ddlx);
        masterMap.put("oea01", xsdddh);
        masterMap.put("oea02", ddrq);
        masterMap.put("oea03", ckkhbh);
        masterMap.put("oea04", shkh);
        masterMap.put("oea05", "1");
        masterMap.put("oea08", xsddxz);
        masterMap.put("oea10", khddh);
        masterMap.put("oea1008", hszje);
        masterMap.put("oea14", sqrgh);
        masterMap.put("oea15", sqbmbm);
        masterMap.put("oea21", sz);
        masterMap.put("oea23", bz);
        masterMap.put("oea24", hl2);
        masterMap.put("oea31", jgtj);
        masterMap.put("oea32", sktj);
        masterMap.put("oea032", shkhjc);
        masterMap.put("oea61", sqzje);
        masterMap.put("oeaud07", hth);
        masterMap.put("oea11", zlly);
        masterMap.put("oea12", xsdddh);
        out.print(">>>>>>>>>>>>主表字段信息=" + masterMap.toString()+"<br/>");
        List<Map<String, String>> detailMapList = new ArrayList<>();
        rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?  ", mainid);
        int count = 1;
        while (rs.next()) {
            Map<String, String> map = new HashMap<>();
            map.put("oeb03", count+"");
            map.put("oeb04", Util.null2String(rs.getString("ljbh")));
            map.put("oeb05", Util.null2String(rs.getString("xsdw")));
            map.put("oeb06", Util.null2String(rs.getString("cpxh")));
            map.put("oeb12", Util.null2String(rs.getString("xssl")));
            map.put("oeb13", Util.null2String(rs.getString("dj")));
            map.put("oeb14", Util.null2String(rs.getString("sqje")));
            map.put("oeb14t", Util.null2String(rs.getString("hsje")));
            map.put("oeb15", Util.null2String(rs.getString("ydjhrq")));
            map.put("oeb16", Util.null2String(rs.getString("pdjhrq")));
            map.put("oeb11", Util.null2String(rs.getString("khcpbh")));
            out.print(">>>>>>>>>>>>明细字段信息=" + map.toString()+"<br/>");
            detailMapList.add(map);
            count++;
        }
        JSONObject resultJson = WorkRequestHandle(method, masterMap, detailMapList);
        out.print(">>>>>>>>>>>>接口返回结果=" + resultJson.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }

%>
<%!
    /**
     * @Description: 通用接口调用接口
     * @Param: [paramJson]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/6/20
     */
    public static JSONObject WorkRequestHandle(String method, Map<String, String> masterMap, List<Map<String, String>> detailMapList) {
        JSONObject returnJson = new JSONObject();
        StringBuilder masterRecord = new StringBuilder("<Record>");
        for (Map.Entry<String, String> entry : masterMap.entrySet()) {
            masterRecord.append("<Field name='" + entry.getKey() + "' value='" + entry.getValue() + "'/>");
        }
        masterRecord.append("</Record>");
        String master = "<Master name='oea_file'>" + masterRecord + "</Master>";

        StringBuilder details = new StringBuilder("<Detail name='oeb_file'>");
        for (Map<String, String> stringMap : detailMapList) {
            StringBuilder detailRecord = new StringBuilder("<Record>");
            for (Map.Entry<String, String> entry : stringMap.entrySet()) {
                detailRecord.append("<Field name='" + entry.getKey() + "' value='" + entry.getValue() + "'/>");
            }
            detailRecord.append("</Record>");
            details.append(detailRecord);
        }
        details.append("</Detail>");
        String params = "<Document><RecordSet id='1'>" + master + details + "</RecordSet></Document>";
        try {
            String message = InvokeHandle(method, params);
            Document xmlDocument = DocumentHelper.parseText(message);
            if (xmlDocument != null) {
                Element rootElement = xmlDocument.getRootElement();
                System.out.println(rootElement.asXML());
                Element Status = rootElement
                        .element("Body")
                        .element(method + "Response")
                        .element("response")
                        .element("Response")
                        .element("Execution").element("Status");
                String code = Status.attributeValue("code");
                String description = Status.attributeValue("description");
                returnJson.put("code", code);
                returnJson.put("description", description);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnJson;
    }

    /**
     * @Description: 通用调用接口
     * @Param: [paramJson]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/6/20
     */
    public static String InvokeHandle(String method, String bodyXML) {
        String wsdl = "http://10.0.3.51/web/ws/r/aws_ttsrv2";
        String Header_beg = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"" +
                " xmlns:tip=\"http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay\">\n<soapenv:Header/>\n";
        String Header_end = "</soapenv:Envelope>\n";
        String Body_beg = "<soapenv:Body>\n";
        String Body_end = "</soapenv:Body>\n";
        String Method_beg = "<tip:" + method + "Request>\n";
        String Method_end = "</tip:" + method + "Request>\n";
        String request_beg = "<tip:request><![CDATA[<Request>\n";
        String request_end = "</Request>]]></tip:request>\n";
        String Access = "<Access>\n" +
                "        <Authentication user=\"apiadmin\" password=\"apiadmin\"/>\n" +
                "        <Connection application=\"OA\" source=\"127.0.0.1\"/>\n" +
                "        <Organization name=\"ZWGK99\"/>\n" +
                "        <Locale language=\"zh_cn\"/>\n" +
                "    </Access>\n";
        String Document = "<RequestContent>\n" +
                "        <Parameter/>" + bodyXML + "\n</RequestContent>";

        StringBuilder templateXML = new StringBuilder(Header_beg);
        templateXML.append(Body_beg);
        templateXML.append(Method_beg);
        templateXML.append(request_beg);
        templateXML.append(Access);
        templateXML.append(Document);
        templateXML.append(request_end);
        templateXML.append(Method_end);
        templateXML.append(Body_end);
        templateXML.append(Header_end);
        String xml = templateXML.toString();
        new BaseBean().writeLog(">>>>>>InvokeHandle>>>>method"+method+",xml="+xml);
        return doPostWebServiceURL(wsdl, xml, "\"\"");
    }

    /**
     * @param url
     * @param xmlParams
     * @return
     * @throws Exception
     */
    public static String doPostWebServiceURL(String url, String xmlParams, String soapAction) {
        String result = "";
        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(url);
        try {
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
            httpPost.setHeader("SOAPAction", soapAction);
            StringEntity data = new StringEntity(xmlParams, "UTF-8");
            httpPost.setEntity(data);
            CloseableHttpResponse response = closeableHttpClient
                    .execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                result = EntityUtils.toString(httpEntity, "UTF-8");
                result = result.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
            }
        } catch (Exception e) {
            System.out.println("调用远程WebService接口异常:{}" + e);
        } finally {
            IOUtils.closeQuietly(closeableHttpClient);
        }
        return result;
    }

%>